# -*- coding: utf-8 -*-

import glob
import codecs
import string
import re
import d15n_routines as r

# binary end of line to be used
n = '\x0D\x0A'
d = open('#cleanup_email_dropped_lines.dat', 'wb')

counters = {'cnt_all_lines':0, 'cnt_@':0, 'cnt_dot':0}

for f in glob.glob('POC_ID_email_v3.csv'):
    print f
    i = open(f, 'rb')
    o = open('_cleanup_' + f[:-4] + '.csv', 'wb')

    counters['cnt_all_lines'] = 0
    for l in i.readlines():
        counters['cnt_all_lines'] += 1
        l = r.remove_eol(l)

        if len(l) == 0:
            # skip empty rows
            d.write(f + ' -- ' + l + n)
            pass
        elif counters['cnt_all_lines'] == 1: # skip headers
            pass
            d.write(f + ' -- ' + l + n)
        else:
            # remove multiple spaces, comas
            l = re.sub('[ ,]', '', l)
            l = r.ascii_upper(l)
            cols = string.split(l, '|')
            
            id = cols[0]; em = cols[1]
            id = id.strip(); em = em.strip()

            em_alias = ''; em_domain = ''
            if len(em) > 1 and string.find(em, '@') <> -1:
                em_alias, em_domain = string.split(em, '@', 1)
            elif em == '@':
                counters['cnt_@'] += 1
            elif em == '.':
                counters['cnt_dot'] += 1
            elif em in (''):
                em = '<EMPTY>'

            if em_alias <> '' and em_domain <> '':
                res = "%s\t%s\t%s\t@%s" % (id, em, em_alias, em_domain)
            else:
                res = "%s\t%s\t%s\t%s" % (id, em, '', '')

            o.write(res + n)
    i.close(); o.close()
d.close()

print "counters['cnt_all_lines']", str(counters['cnt_all_lines'])
print "counters['cnt_@']", str(counters['cnt_@'])
print "counters['cnt_dot']", str(counters['cnt_dot'])
